#include <bits/stdc++.h>
using namespace std;

int a[2001], dp[2001][2][2001];
int main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    int t;
    cin >> t;
    while (t--)
    {
        memset(dp, 0, sizeof dp);
        int n;
        cin >> n;
        for (int i = 1; i <= n; i++) cin >> a[i];
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= i - 2; j++)
            {
                dp[i][0][i - 1] = max(dp[i][0][i - 1], dp[i - 1][1][j] + ((a[j] == a[i]) ? a[i] : 0));
                dp[i][0][j] = max(dp[i][0][j], dp[i - 1][0][j] + ((a[i - 1] == a[i]) ? a[i] : 0));
                dp[i][1][i - 1] = max(dp[i][1][i - 1], dp[i - 1][0][j] + ((a[j] == a[i]) ? a[i] : 0));
                dp[i][1][j] = max(dp[i][1][j], dp[i - 1][1][j] + ((a[i - 1] == a[i]) ? a[i] : 0));
            }
        }
        int ans = 0;
        for (int i = 1; i < n; i++) ans = max(ans, max(dp[n][0][i], dp[n][1][i]));
        cout << ans << endl;
    }
    return 0;
}
